package com.boots.wx.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/wxPay")
public class WxPayController {

    @Resource
    private NativePayService nativePayService;

    @RequestMapping("/pay")
    public String pay() {
            PrepayRequest request = new PrepayRequest();
            Amount amount = new Amount();
            //设置金额   单位是： 分
            amount.setTotal(100);
            request.setAmount(amount);
            //appid
            request.setAppid("xx");
            //商户号
            request.setMchid("xx");
            //商品标题
            request.setDescription("测试商品标题");
            //回调url
            request.setNotifyUrl("https://notify_url");
            //商户订单号
            request.setOutTradeNo("out_trade_no_0031");
            // 调用下单方法，得到应答
            PrepayResponse response = nativePayService.prepay(request);
            // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
            QrCodeUtil.generatePng(response.getCodeUrl(), 300, 300);
            System.out.println(response.getCodeUrl());
            return response.getCodeUrl();
    }
}
